package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysKeyDida;
import com.ruoyi.system.domain.SysKeyDida;
import com.ruoyi.system.mapper.SysKeyDidaMapper;
import com.ruoyi.system.service.ISysDidaService;
import com.ruoyi.system.utils.NoCreateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ISysDidaServiceImpl implements ISysDidaService {

    @Resource
    private SysKeyDidaMapper sysKeyDidaMapper;

    @Override
    public List<SysKeyDida> selectSysKeyDidaList(SysKeyDida sysKeyDida) {
       return sysKeyDidaMapper.selectSysKeyYkList(sysKeyDida);
    }

    @Override
    public AjaxResult addKey(SysKeyDida sysKey) {
        if (sysKey.getNumber() <= 0) {
            return AjaxResult.error("数量不能低于1张");
        }
        if (sysKey.getNumber() > 500) {
            return AjaxResult.error("单次数量不能大于500张");
        }
        StringBuilder stringBuffer = new StringBuilder();
        for (int i = 0; i < sysKey.getNumber(); i++) {
            sysKey.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
            sysKey.setIsActivated("0");
            sysKey.setAccount("未使用");
            sysKey.setStatus("0");
            String key = NoCreateUtil.getID() + NoCreateUtil.getNonce_str();
            switch (sysKey.getKeyTypes()) {
                case "0":
                    sysKey.setKeyNumber(key);
                    break;
                case "1":
                    sysKey.setKeyNumber(key);
                    break;
                case "2":
                    sysKey.setKeyNumber(key);
                    break;
                case "3":
                    sysKey.setKeyNumber(key);
                    break;
            }
            stringBuffer.append(sysKey.getKeyNumber()).append("\n");
            sysKeyDidaMapper.insertSysKeyYk(sysKey);
        }
        return AjaxResult.success("新增成功", stringBuffer.toString());
    }

    @Override
    public AjaxResult delKey(SysKeyDida SysKeyDida) {
        sysKeyDidaMapper.deleteSysKeyYkById(SysKeyDida.getId());
        return AjaxResult.success("删除成功");
    }

    @Override
    public AjaxResult shutDown(String key) {
        // 查询当前卡密状态
        SysKeyDida SysKeyDida = sysKeyDidaMapper.selectSysKeyYkById(Long.parseLong(key));
        SysKeyDida.setStatus(SysKeyDida.getStatus().equals("1") ? "0" : "1");
        sysKeyDidaMapper.shutDownYk(SysKeyDida);
        return AjaxResult.success(SysKeyDida.getStatus().equals("1") ? "封停成功" : "解封成功");
    }

    @Override
    public AjaxResult noBind(String key) {
        // 查询
        sysKeyDidaMapper.noBindYk(key);
        return AjaxResult.success("解绑成功");
    }
}
